package huaweiod.first;

import java.util.Scanner;

/**
 * 华为OD机试真题 Java 实现【开心消消乐】【2022.11 Q4 新题】3 3
 * 1 0 1
 * 0 1 0
 * 1 0 1
 * 1
 */
public class A_2023_59_HappyXiaoXiaoLe {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int row = in.nextInt();
        int col = in.nextInt();
        int[][] matrix = new int[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                matrix[i][j] = in.nextInt();
            }
        }
        int ans = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] == 1) {
                    bfs(matrix, i, j);
                    ans++;
                }
            }
        }
        System.out.println(ans);
    }

    // 8个方位
    public static int[][] dist = {{-1, 1}, {0, 1}, {1, 1}, {-1, 0}, {1, 0}, {-1, -1}, {0, -1}, {1, -1}};

    public static void bfs(int[][] matrix, int x, int y) {
        matrix[x][y] = 0;
        for (int[] ints : dist) {
            int newX = x + ints[0];
            int newY = y + ints[1];
            if (newX >= 0 && newX < matrix.length && newY >= 0 && newY < matrix[0].length
                    && matrix[newX][newY] == 1) {
                bfs(matrix, newX, newY);
            }
        }
    }
}
